﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DenderaMvcWeb.Models;

namespace DenderaMvcWeb.Controllers
{
    public class FilatoController : Controller
    {
        private DenderaEntities db = new DenderaEntities();

        //
        // GET: /Filato/

        public ActionResult Index(string id)
        {
            var filatoes = db.filatoes.Include(f => f.anagrafica);
            return View(filatoes.ToList());
        }

        //public ActionResult SearchIndex(string movieGenre, string searchString)
        //{
        //    var GenreLst = new List<string>();

        //    var GenreQry = from d in db.Movies
        //                   orderby d.Genre
        //                   select d.Genre;
        //    GenreLst.AddRange(GenreQry.Distinct());
        //    ViewBag.movieGenre = new SelectList(GenreLst);

        //    var movies = from m in db.Movies
        //                 select m;

        //    if (!String.IsNullOrEmpty(searchString))
        //    {
        //        movies = movies.Where(s => s.Title.Contains(searchString));
        //    }

        //    if (string.IsNullOrEmpty(movieGenre))
        //        return View(movies);
        //    else
        //    {
        //        return View(movies.Where(x => x.Genre == movieGenre));
        //    }

        //}
        //public ActionResult SearchString(string SearchString)
        //{
        //    //string SearchString = id;
        //    var filato = from m in db.filatoes
        //                 select m;

        //    if (!String.IsNullOrEmpty(SearchString))
        //    {
        //        filato = filato.Where(s => s.label.Contains(SearchString));
        //    }

        //    return View(filato);
        //}


        //
        // GET: /Filato/Details/5

        public ActionResult Details(int id = 0)
        {
            filato filato = db.filatoes.Find(id);
            if (filato == null)
            {
                return HttpNotFound();
            }
            return View(filato);
        }

        //
        // GET: /Filato/Create

        public ActionResult Create()
        {
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "ragione_soc");
            return View();
        }

        //
        // POST: /Filato/Create

        [HttpPost]
        public ActionResult Create(filato filato)
        {
            if (ModelState.IsValid)
            {
                db.filatoes.Add(filato);
                filato.ultima_modifica = DateTime.Now;//data di default
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "ragione_soc", filato.id_anag);
            return View(filato);
        }

        //
        // GET: /Filato/Edit/5

        public ActionResult Edit(int id = 0)
        {
            filato filato = db.filatoes.Find(id);
            if (filato == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "ragione_soc", filato.id_anag);
            return View(filato);
        }

        //
        // POST: /Filato/Edit/5

        [HttpPost]
        public ActionResult Edit(filato filato)
        {
            if (ModelState.IsValid)
            {
                db.Entry(filato).State = EntityState.Modified;
                filato.ultima_modifica = DateTime.Now;//data di default
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_anag = new SelectList(db.anagraficas, "id_anag", "ragione_soc", filato.id_anag);
            return View(filato);
        }

        //
        // GET: /Filato/Delete/5

        public ActionResult Delete(int id = 0)
        {
            filato filato = db.filatoes.Find(id);
            if (filato == null)
            {
                return HttpNotFound();
            }
            return View(filato);
        }

        //
        // POST: /Filato/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            filato filato = db.filatoes.Find(id);
            db.filatoes.Remove(filato);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}